package com.huawei.hms.framework.network.restclient.hwhttp.trans;

import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileBinary implements Binary {
    public static final String HEAD_VALUE_CONTENT_TYPE_OCTET_STREAM = "application/octet-stream";
    private static final String TAG = "FileBinary";
    private File file;
    private String fileName;
    private String mimeType;

    public FileBinary(File file) {
        this(file, file.getName(), null);
    }

    public FileBinary(File file, String str, String str2) {
        if (file == null) {
            throw new IllegalArgumentException("File is null: " + str);
        } else if (file.exists()) {
            this.file = file;
            this.fileName = str;
            this.mimeType = str2;
        } else {
            throw new IllegalArgumentException("File not found: " + str);
        }
    }

    public long getLenth() {
        return this.file.length();
    }

    public InputStream getInputStream() throws IOException {
        return new FileInputStream(this.file);
    }

    /* JADX WARNING: inconsistent code. */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public void onWriteBinary(java.io.OutputStream r6) {
        /*
        r5 = this;
        r0 = 0;
        r0 = r5.getInputStream();	 Catch:{ IOException -> 0x001b, all -> 0x002b }
        if (r0 != 0) goto L_0x000b;
    L_0x0007:
        com.huawei.hms.framework.common.IoUtils.closeSecure(r0);
    L_0x000a:
        return;
    L_0x000b:
        r1 = 8192; // 0x2000 float:1.14794E-41 double:4.0474E-320;
        r1 = new byte[r1];	 Catch:{ IOException -> 0x001b }
    L_0x000f:
        r2 = r0.read(r1);	 Catch:{ IOException -> 0x001b }
        r3 = -1;
        if (r2 == r3) goto L_0x0027;
    L_0x0016:
        r3 = 0;
        r6.write(r1, r3, r2);	 Catch:{ IOException -> 0x001b }
        goto L_0x000f;
    L_0x001b:
        r1 = move-exception;
        r1 = "FileBinary";
        r2 = "the file is reading but has error!";
        com.huawei.hms.framework.common.Logger.w(r1, r2);	 Catch:{ all -> 0x0033 }
        com.huawei.hms.framework.common.IoUtils.closeSecure(r0);
        goto L_0x000a;
    L_0x0027:
        com.huawei.hms.framework.common.IoUtils.closeSecure(r0);
        goto L_0x000a;
    L_0x002b:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
    L_0x002f:
        com.huawei.hms.framework.common.IoUtils.closeSecure(r1);
        throw r0;
    L_0x0033:
        r1 = move-exception;
        r4 = r1;
        r1 = r0;
        r0 = r4;
        goto L_0x002f;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.huawei.hms.framework.network.restclient.hwhttp.trans.FileBinary.onWriteBinary(java.io.OutputStream):void");
    }

    public String getFileName() {
        if (TextUtils.isEmpty(this.fileName)) {
            this.fileName = Long.toString(System.currentTimeMillis());
        }
        return this.fileName;
    }

    public String getMimeType() {
        Object fileName = getFileName();
        if (TextUtils.isEmpty(this.mimeType) && !TextUtils.isEmpty(fileName)) {
            this.mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(fileName));
        }
        if (TextUtils.isEmpty(this.mimeType)) {
            this.mimeType = HEAD_VALUE_CONTENT_TYPE_OCTET_STREAM;
        }
        return this.mimeType;
    }
}
